﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformScaleBoxUnit : MonoBehaviour {

    [SerializeField]
    float absolutelySize = 1;

    public VoxelDirection direction;

    Camera mainCamera;

    [SerializeField] MeshRenderer[] meshRenderers;
    [SerializeField] Material slelectMaterial;
    [SerializeField] Material myMaterial;
    void Awake()
    {
        mainCamera = Camera.main;
        myMaterial = meshRenderers[0].material;
    }

    void Start () {
		
	}
	
	void Update () {
        var va = mainCamera.WorldToScreenPoint(this.transform.position);
        var vb = mainCamera.WorldToScreenPoint((this.transform.position + mainCamera.transform.rotation * new Vector3(0, absolutelySize, 0)));
        this.transform.localScale = new Vector3(1, 1, 1) * (Screen.height / Vector3.Distance(va, vb)) / 5f;
    }

    void LateUpdate()
    {
    }
    Vector3 moveStartPosition;
    public void Select()
    {
        foreach (var item in meshRenderers)
        {
            item.material = slelectMaterial;
        }
        if (direction != TransformScaleBox.Thas.direction && TransformScaleBox.selectUnit)
        {
            TransformScaleBox.selectUnit.NoSelect();
        }
        TransformScaleBox.selectUnit = this;
        TransformScaleBox.Thas.direction = direction;
        moveStartPosition = this.transform.position;
    }
    public void NoSelect()
    {
        foreach (var item in meshRenderers)
        {
            item.material = myMaterial;
        }
        if (TransformScaleBox.selectUnit)
        {
            TransformScaleBox.selectUnit = null;
        }
    }

    public void Move(Vector3 cursorPos)
    {
        cursorPos = Camera.main.transform.rotation * cursorPos;
        switch (direction)
        {
            case VoxelDirection.Right:
                this.transform.position = moveStartPosition + new Vector3((cursorPos.x * this.transform.localScale.x / 7f) , 0, 0);
                break;
            case VoxelDirection.Left:
                this.transform.position = moveStartPosition + new Vector3((cursorPos.x * this.transform.localScale.x / 7f), 0, 0);
                break;
            case VoxelDirection.Up:
                this.transform.position = moveStartPosition + new Vector3(0, cursorPos.y * this.transform.localScale.y / 7f, 0);
                break;
            case VoxelDirection.Down:
                this.transform.position = moveStartPosition + new Vector3(0, cursorPos.y * this.transform.localScale.y / 7f, 0);
                break;
            case VoxelDirection.Front:
                this.transform.position = moveStartPosition + new Vector3(0, 0, cursorPos.z * this.transform.localScale.z / 7f);
                break;
            case VoxelDirection.Break:
                this.transform.position = moveStartPosition + new Vector3(0, 0, cursorPos.z * this.transform.localScale.z / 7f);
                break;
            default:
                break;
        }
    }
}
